<template>
<!--  <page-header-wrapper>-->
<!--    <template v-slot:content>-->
<!--    </template>-->
    <div>
      <a-row :gutter="24" type="flex">
        <a-col :xl="24" :lg="24" :md="24" :sm="24" :xs="24">
          <a-card
            class="project-list"
            style="margin-bottom: 24px;"
            :bordered="false"
            title="违章车辆检测"
            :body-style="{ padding: 10 }"
          >
            <div class="account-settings-info-view">
              <a-row :gutter="16" type="flex">
                <a-col :order="1" :md="24" :lg="8" :style="{ minHeight: '100px' }">
                  <template>
                    <h2>城市</h2>
                    <a-space>
                      <a-select
                        ref="select"
                        :value="regionPId"
                        style="width: 120px"
                        @change="handleChangeP"
                      >
                        <a-select-option :value="item.id" v-for="(item, idx) in regionPList" :key="idx">{{ item.name }}</a-select-option>
                      </a-select>
                    </a-space>
                    <h2 style="margin-top: 10px">区/县级市</h2>
                    <a-space>
                      <a-select ref="select" :value="regionId" style="width: 120px" @change="handleChange">
                        <a-select-option :value="item.id" v-for="(item, idx) in regionList" :key="idx">{{ item.name }}</a-select-option>
                      </a-select>
                    </a-space>
                  </template>
                </a-col>
                <a-col :order="1" :md="24" :lg="8" :style="{ minHeight: '100px' }">
                  <div class="ant-upload-preview" @click="showUpload">
                    <a-icon type="cloud-upload-o" class="upload-icon"/>
                    <div class="mask">
                      <a-icon type="plus"/>
                    </div>
                    <img src="/uploadImg.jpeg"/>
                  </div>
                </a-col>
              </a-row>

              <upload ref="modal" @ok="setResult"/>
            </div>
          </a-card>
        </a-col>
      </a-row>
      <a-row :gutter="24" v-if="list.length > 0">
        <a-col :xl="24" :lg="24" :md="24" :sm="24" :xs="24">
          <a-card
            class="project-list"
            style="margin-bottom: 24px;"
            :bordered="false"
            title="识别结果"
            :body-style="{ padding: 10 }"
          >
            <div class="result-wrap">
              <div class="identify-item" v-for="(item, index) in list" :key="index">
                <div class="left-cnt">
                  <img :src="webUrl + item.imageBase64" alt="">
                  <div class="mark" v-for="(postion, idx) in item.results" :key="idx" :style="'left:' + (parseInt(postion.location.left) + 3) + 'px;top:' + (parseInt(postion.location.top)+ 5) + 'px;' ">{{ idx + 1 }}</div>
                </div>
                <div class="right-cnt">
                  <div>违规车辆数量：{{ item.results.length }}</div>
                  <div class="" v-for="(result, i) in item.results" :key="i">
                    <div>违规车辆<span style="color: red;font-weight: 700">{{ i + 1 }}</span>  检测score：{{ result.score || '' }}</div>
                  </div>
                </div>
              </div>
            </div>
          </a-card>
        </a-col>
      </a-row>
    </div>
<!--  </page-header-wrapper>-->
</template>

<script>
import Upload from './Upload'
import { baseMixin } from '@/store/app-mixin'
import { ref } from 'vue'

export default {
  mixins: [baseMixin],
  components: {
    Upload
  },
  data() {
    return {
      // cropper
      preview: {},
      option: {
        info: true,
        size: 1,
        outputType: 'jpeg',
        canScale: false,
        autoCrop: true,
        // 只有自动截图开启 宽度高度才生效
        autoCropWidth: 180,
        autoCropHeight: 180,
        fixedBox: true,
        // 开启宽度和高度比例
        fixed: true,
        fixedNumber: [1, 1]
      },
      list: [],
      // 位置适配
      fixPositionPx: 5,
      webUrl: 'http://localhost:8000/upload/',
      regionPId: '',
      regionPList: [],
      regionId: '',
      regionList: []
    }
  },
  methods: {
    setResult(result) {
      this.list.push(result)
    },
    init() {
      this.$loading.show()
      this.$http.get('http://localhost:8003/fj-api/region/list')
        .then((response) => {
          console.log(response)
          this.regionPList = response.data
          this.$loading.hide()
        }).catch(() => {
        this.$loading.hide()
      })
    },
    handleChangeP(item) {
      console.log(item)
      this.regionPId = item
      this.$http.get('http://localhost:8003/fj-api/region/list?p_id=' + item)
        .then((response) => {
          console.log(response)
          this.regionList = response.data
        }).catch(() => {
      })
    },
    handleChange(item) {
      this.regionId = item
    },
    showUpload() {
      console.log(this.regionId)
      if (!this.regionId) {
        this.$message.error('请先选择街道')
        return
      }
      this.$refs.modal.show(this.regionId, this.regionPId)
    }
  },
  created() {
    this.init()
  }
}
</script>

<style lang="less" scoped>

.avatar-upload-wrapper {
  height: 200px;
  width: 100%;
}

.ant-upload-preview {
  position: relative;
  margin: 0 auto;
  width: 100%;
  max-width: 180px;
  border-radius: 50%;
  box-shadow: 0 0 4px #ccc;

  .upload-icon {
    position: absolute;
    top: 0;
    right: 10px;
    font-size: 1.4rem;
    padding: 0.5rem;
    background: rgba(222, 221, 221, 0.7);
    border-radius: 50%;
    border: 1px solid rgba(0, 0, 0, 0.2);
  }

  .mask {
    opacity: 0;
    position: absolute;
    background: rgba(0, 0, 0, 0.4);
    cursor: pointer;
    transition: opacity 0.4s;

    &:hover {
      opacity: 1;
    }

    i {
      font-size: 2rem;
      position: absolute;
      top: 50%;
      left: 50%;
      margin-left: -1rem;
      margin-top: -1rem;
      color: #d6d6d6;
    }
  }

  img, .mask {
    width: 100%;
    max-width: 180px;
    height: 100%;
    border-radius: 50%;
    overflow: hidden;
  }
}

.result-wrap {
  .identify-item {
    float: left;
    padding-bottom: 10px;
    border-bottom: 1px solid #efdfc5;
    width: 100%;
    padding-top: 10px;
    .left-cnt {
      width: 60%;
      float: left;
      position: relative;

      .mark {
        position: absolute;
        color: red;
        font-size: 15px;
        font-weight: 700;
      }
    }

    .right-cnt {
      width: 40%;
      float: right;
      text-align: center;
      position: relative;
    }
  }
}
</style>
